//
//  ByApiCommitOrder.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByApiCommitOrder.h"

#import "ByModelOrderPay.h"         // 订单支付model

@implementation ByApiCommitOrder

// 创建订单
+ (void)createOrder:(ByNetParamsModel *)model
            success:(void(^)(ByNetBaseResult *result))success
            failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Order_create";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.ids forKey:@"ids"];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.address_id forKey:@"address_id"];
    if (model.del) {
        [params setObject:model.del forKey:@"del"];
    }
    if (model.note) {
        [params setObject:model.note forKey:@"note"];
    }
    if (model.s_id) {
        [params setObject:model.s_id forKey:@"s_id"];
    }
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           NSLog(@"responseObject = %@", responseObject);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"创建成功";
           ByModelOrderPay *model = [[ByModelOrderPay alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 创建订单 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure){
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}


@end
